package com.ld.common.util.filelistener.jnotify.impl;

import com.ld.common.util.filelistener.jnotify.FileChangedListenerManager;
import com.ld.common.util.filelistener.jnotify.FileChangedListenerModel;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.core.util.PathUtil;

import lombok.extern.slf4j.Slf4j;
import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyListener;

@Slf4j
public class ProjectFileListenerUtil {
    public static final FileChangedListenerModel LISTENER; // menus缓存
    public static final String NAME = "项目文件监控";
    public static final String KEY_CODE = "projectFileListener";
    public static final int MASK = JNotify.FILE_CREATED | JNotify.FILE_DELETED | JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED;
    public static final String PATH = PathUtil.getProjectPath();

    public static final JNotifyListener listenerHandler = new JNotifyListener() {

        @Override
        public void fileRenamed(int wd, String rootPath, String oldName, String newName) {
            log.warn("重命名 " + rootPath + " : " + oldName + " -> " + newName);
        }

        @Override
        public void fileModified(int wd, String rootPath, String name) {
            log.warn("修改 " + rootPath + " : " + name);
        }

        @Override
        public void fileDeleted(int wd, String rootPath, String name) {
            log.warn("删除 " + rootPath + " : " + name); // 有可能是目录，已经删除无法判断
        }

        @Override
        public void fileCreated(int wd, String rootPath, String name) {
            log.warn("新增 " + rootPath + " : " + name);
        }
    };

    static {
        LISTENER = new FileChangedListenerModel(NAME, KEY_CODE, PATH, MASK, true, listenerHandler);
    }

    public static Result register() {
        return FileChangedListenerManager.register(LISTENER);
    }

}
